<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发票图像上传</title>
</head>
<body>
<h1>发票图像上传</h1>
<form id="uploadForm" enctype="multipart/form-data">
    <input type="file" id="fileInput" name="file" accept="image/*" required>
    <button type="submit">上传</button>
</form>
<div id="result"></div>
<script>
    document.getElementById('uploadForm').addEventListener('submit', async (event) => {
        event.preventDefault();

        const fileInput = document.getElementById('fileInput');
        const file = fileInput.files[0];

        if (!file) {
            alert('Please select a file!');
            return;
        }

        const formData = new FormData();
        formData.append('file', file);

        try {
            const response = await fetch('http://localhost:8080/upload', {
                method: 'POST',
                body: formData
            });

            if (response.ok) {
                const result = await response.json();
                document.getElementById('result').innerText = JSON.stringify(result, null, 2);
            } else {
                alert('Failed to upload image!');
            }
        } catch (error) {
            console.error('Error:', error);
            alert('An error occurred!');
        }
    });
</script>
</body>
</html>
